<template>
<div class="addSubject">

    <el-button type="primary" size="mini" style="margin-right: 10px" @click="relevancySubjectShow = true">
        <slot>新增经营信息</slot>
    </el-button>

    <el-dialog title="新增生产主体" :append-to-body="true" :visible.sync="AddDialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false">
        <el-card>
            <el-tabs v-model="typeClass" stretch>
                <el-tab-pane label="个体户" name="个体户">
                    <Individual editor="新增" v-if="typeClass === '个体户'" @on-close="closeAddModel()" @success="addIndividual"></Individual>
                </el-tab-pane>
                <el-tab-pane label="合作社" name="合作社">
                    <Cooperation editor="新增" v-if="typeClass === '合作社'" @on-close="closeAddModel()" @success="addIndividual"></Cooperation>
                </el-tab-pane>
                <el-tab-pane label="企业" name="企业">
                    <Enterprise editor="新增" v-if="typeClass === '企业'" @on-close="closeAddModel()" @success="addIndividual"></Enterprise>
                </el-tab-pane>
            </el-tabs>
        </el-card>
        <!--		<el-button @click="AddDialogFormVisible = false" type="default" class="btn-block" style="margin-top: 15px">-->
        <!--		关闭窗口</el-button>-->
    </el-dialog>

    <el-dialog title="查询主体" :close-on-click-modal="true" :visible.sync="searchSubjectShow" append-to-body width="70%">
        <el-card style="margin-bottom: 20px">
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input type="text" placeholder="请输入主体名称（模糊搜索）" v-model="keywords"></el-input>
                </el-col>
                <el-col :span="6">
                    <el-button type="primary" @click.native="filterSubjectForm">查询</el-button>
                    <el-button type="info" @click.native="AddDialogFormVisible = true">新增生产主体</el-button>
                </el-col>
            </el-row>
        </el-card>

        <el-card v-if="SubjectForm.length">
            <el-row :gutter="20">
                <el-col :span="8" v-for="(item, index) in SubjectForm" :key="index">
                    <el-card shadow="never" class="result" @click.native="insertion(item)" :key="index">
                        <h4 class="title">
                            <span><span>主体名称:</span>{{ item.subjectName }} </span>
                            <div>
                                <el-button type="success" size="mini" @click.native="filterSubjectForm">选择</el-button>
                                <el-button type="danger" size="mini" @click.stop="handleClick(item, 'edit')">编辑</el-button>
                            </div>
                        </h4>
                        <p v-if="item.subjectTypeName === '个体户'">
                            <span>公民身份号码:</span>{{ item.subjectCard }}
                        </p>
                        <p v-else><span>主体组织机构代码:</span>{{ item.subjectCard }}</p>
                        <p><span>主体类别:</span>{{ item.subjectTypeName }}</p>
                        <p><span>手机号码:</span>{{ item.subjectTele }}</p>
                        <p><span>主体地址:</span>{{ item.subjectAddr }}</p>
                        <p><span>主体基地:</span>{{ item.subjectBase }}</p>
                        <p :title="item.subjectIntro">
                            <span>主体介绍:</span>{{
                  item.subjectIntro.length > 70
                    ? item.subjectIntro.substring(0, 70) + "..."
                    : item.subjectIntro
                }}
                        </p>
                    </el-card>
                </el-col>
            </el-row>

            <div class="dialog-footer" style="float: right; padding: 15px 0; display: none">
                <el-button type="info" @click="searchSubjectShow = false">关闭查询窗口</el-button>
            </div>
        </el-card>
    </el-dialog>

    <el-dialog title="搜索三资数据" :close-on-click-modal="true" :visible.sync="searchThreeShow" append-to-body width="65%">
        <el-form :model="form" autocomplete="off">
            <el-form-item label="搜索条件">
                <el-input v-model="ThreeKeyword" placeholder="支持对合同编号/经营主体/所属单位/项目名称（土名）的搜索">
                    <el-button slot="append" type="primary" @click="searchThree">搜索</el-button>
                </el-input>
            </el-form-item>
        </el-form>
        <template>
            <el-table :data="ThreeData" style="width: 100%">
                <el-table-column prop="htbh" label="合同编号"  >
                </el-table-column>
                <el-table-column prop="czdw" label="经营主体" >
                </el-table-column>
                <el-table-column prop="dwmc" label="所属单位" >
                </el-table-column>

                <el-table-column prop="xmmc" label="项目名称"  >
                </el-table-column>
                <el-table-column prop="zdmj" label="占地面积" >
                </el-table-column>

                <el-table-column prop="address" label="操作">
                    <template slot-scope="scope">
                        <el-button @click="handleClick2(scope.row)" type="text" size="small">选择</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination layout="total, sizes, prev, next, pager, jumper" @size-change="sizeChange" @current-change="currentChange" :page-sizes="[10, 20,30,50]" :page-size="10" :total="total">
            </el-pagination>

        </template>
    </el-dialog>

    <el-dialog title="经营信息" :append-to-body="true" :visible.sync="relevancySubjectShow">

        <el-form :model="form" autocomplete="off" v-if="relevancySubjectShow">
            <span v-show="false">{{landDataSubjectId}}</span>
            <el-form-item label="经营主体">
                <el-input v-model="form.subjectName" :readonly="true">
                    <el-button slot="append" :style="append" @click="searchSubject" type="text">搜索经营主体</el-button>
                </el-input>
            </el-form-item>

            <el-form-item label="所属单位">
                <el-input v-model="form.affiliatedUnit" >
                    <el-button slot="append" :style="append" @click="searchThree" type="text">拉取三资数据</el-button>
                </el-input>
            </el-form-item>

            <el-form-item label="合同编号">
                <el-input v-model="form.contractId" ></el-input>
            </el-form-item>

            <el-form-item label="占用面积(亩)">
                <el-input v-model="form.lsSubjectUseArea" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="年产值(万元)">
                <el-input v-model="form.lsYearValue" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="承包周期">
                <el-date-picker v-model="formDate" type="daterange" unlink-panels range-separator=" 至 " start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" @change="setYear()">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="承包年限">
                <el-input v-model="form.contractYear" :disabled="true" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="所属基地">
                <el-input v-model="form.landSubjectBase" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="基地地址">
                <el-input v-model="form.landSubjectBaseAdd" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="relevancySubjectShow = false">取 消</el-button>
            <el-button type="primary" @click="submit">确 定</el-button>
        </div>
    </el-dialog>

    <el-dialog title="主体详情" :append-to-body="true" :visible.sync="DetailsDialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false">
        <el-card>
            <Look @success="editIndividual" @on-close="DetailsDialogFormVisible = false" :editor="editor" :subjectFormRow="subjectFormRow"></Look>
        </el-card>
        <!--            <el-button @click="DetailsDialogFormVisible = false" type="danger" class="btn-block"-->
        <!--                       style="margin-top: 15px">关闭窗口</el-button>-->
    </el-dialog>
</div>
</template>

<script>
import Individual from "@/views/DataResource/ProductionBase/Individual/Individual.vue";
import Cooperation from "@/views/DataResource/ProductionBase/Cooperation/Cooperation";
import Enterprise from "@/views/DataResource/ProductionBase/Enterprise/Enterprise";
import Look from "@/views/DataResource/ProductionBase/Look/Look";
import {
    formatDate
} from "@/library";
import {
    mapState
} from "vuex";
export default {
    name: "",
    props: {
        landCodeT: {
            type: [String, Array],
            required: true,
        },
        addType: {
            type: Number,
            default: 0,
        },
        subjectTemp: {
            type: Object
        },
        // tableData: {
        // 	type: Array
        // },
        landSubjectId: {
            type: [String, Number],
            default: ''
        }
    },
    components: {
        Individual,
        Cooperation,
        Enterprise,
        Look,
    },
    data() {
        return {
            pageNum: 1,
            size: 10,
            total: 0,
            append: {
                "padding-left": "15px",
                "padding-right": "15px",
                background: "#409EFF",
                color: "#fff",
                "border-top-left-radius": "0",
                "border-bottom-left-radius": "0",
            },
            ThreeKeyword: "",
            ThreeData: [],
            searchThreeShow: false,
            AddDialogFormVisible: false,
            pickerOptions: {
                shortcuts: [{
                        text: "最近一年",
                        onClick(picker) {
                            this.formDate = [];
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90 * 4);
                            picker.$emit("pick", [start, end]);
                        },
                    },
                    {
                        text: "最近三年",
                        onClick(picker) {
                            this.formDate = [];
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90 * 12);
                            picker.$emit("pick", [start, end]);
                        },
                    },
                ],
            },
            typeClass: "个体户",
            keywords: "",
            formLabelWidth: "100px",
            relevancySubjectShow: false,
            searchSubjectShow: false,
            emitForm: {},
            form: {
                date: "",
                contractYear: "",
                subjectName: "",
                lsSubjectUseArea: "",
                lsYearValue: "",
                base: "默认基地",
                lsSubjectName: "",
                contractId: "",
                affiliatedUnit: "",
                contractStartTime: "",
                landSubjectBaseAdd: ""
            },
            params: {
                pageNumber: 1,
                pageSize: 10,
            },
            paging: {},
            SearchFrom: [],
            SubjectForm: [],
            tableData: [],
            List: [],
            SubjectInfoList: [],
            multipleSelection: [],
            AddDialogFormVisible: false,
            DetailsDialogFormVisible: false,
            editor: true,
            subjectFormRow: {},
            formDate: []
        };
    },
    computed: {
        format_Date: {
            get() {
                return this.formDate || this.form.date
            },
            set(val) {
                this.formDate = val
            }
        },
        landDataSubjectId: {
            get() {
                if (!this.landSubjectId) {
                    return false
                }

                this.form = {
                    ...this.subjectTemp
                }
                console.log(this.form);
                if (this.subjectTemp.contractStartTime) {
                    this.formDate = [new Date(this.subjectTemp.contractStartTime), new Date(this.subjectTemp.contractEndTime)]
                }
            
                return this.landSubjectId
            },
            set(id) {
                this.landSubjectId = id
            }
        },
        ...mapState({
            taskObject: (state) => {
                return state.taskmodel.taskObject;
            },
            currentTask: (state) => {
                return state.currentTask.Task;
            },
            userInfo: (state) => {
                return state.User.userInfo;
            },
        }),
    },

    methods: {
        setYear() {
            console.log(this.form.date);
            this.form.contractYear = Math.round(((+new Date(this.formDate[1]) - +new Date(this.formDate[0])) / 1000 / (60 * 60 * 24) / 365).toFixed(2));
        },
        sizeChange(val) {
            console.log(val);
            this.size = val;
            this.searchThree();
        },
        currentChange(val) {
            console.log(val);
            this.pageNum = val;
            this.searchThree();
        },
        searchThree() {
            this.searchThreeShow = true;
            //请求三资数据
            this.$axios.get("/rbac/data/qzcxtcxlsx/get", {
                params: {
                    pageNumber: this.pageNum,
                    pageSize: this.size,
                    keyWord: this.ThreeKeyword
                },
            }).then((res) => {
                if (res.errorCode = "00000") {
                    this.ThreeData = res.data.list;
                    this.total = res.data.total;
                }

            });
        },
        handleClick(row, type) {
            console.log(row);
            this.subjectFormRow = row;
            this.editor = type === "look" ? true : false;
            //this.DetailsDialogFormVisible = true;
        },
        handleClick2(row, type) {
            //此处手动赋值 
            this.form.affiliatedUnit = row.dwmc;
            this.form.contractId = row.htbh;
            this.form.lsSubjectUseArea = row.zdmj;
            this.form.contractStartTime = row.qsrq;
            this.form.landSubjectBaseAdd = row.lxdz;
            //this.form.subjectName=row.czdw;
            this.searchThreeShow = false;
        },
        editIndividual(res) {
            this.DetailsDialogFormVisible = res.visible;
            if (res.code === 200) {
                this.getSubjectInfoList();
                this.filterSubjectForm();
            }
        },
        addIndividual(res) {
            this.AddDialogFormVisible = res.visible;
            if (res.code === 200) {
                this.filterSubjectForm();
            }
        },
        openAdd() {
            
            this.AddDialogFormVisible = true;
        },
        closeAddModel() {
            this.AddDialogFormVisible = false;
            this.filterSubjectForm();
        },
        filterSubjectForm() {
            this.SubjectForm = [];
            var keyword = this.keywords;
            if (keyword) {
                this.$axios
                    .get("/rbac/data/subject/getSubjectInfoList", {
                        params: {
                            "subjectInfo.subjectName": keyword.trim(),
                        },
                    })
                    .then((res) => {
                        if (res.data.list instanceof Array && res.data.list.length) {
                            this.SubjectForm = res.data.list;
                        } else {
                            this.$message({
                                type: "warning",
                                message: "没有查询到对应的主体信息",
                            });
                            this.SubjectForm = [];
                        }
                    });
            } else {
                this.$message({
                    type: "warning",
                    message: "主体名称关键字不能为空",
                });
            }
        },
        insertion(item) {
            this.form.subjectName = item.subjectName;
            this.from = item;
            this.emitForm = item;
            this.searchSubjectShow = false;
        },
        searchSubject() {
            this.searchSubjectShow = true;
        },

        submit() {
            let landCodeT;
            if (this.landCodeT instanceof Array) {
                landCodeT = this.landCodeT.join(",");
            } else {
                landCodeT = this.landCodeT;
            }
            this.$axios
                .post("rbac/data/LandSubject/add", {
                    params: {
                        // lsLandCode: landCodeT,
                        landIds: landCodeT,
                        lsSubjectCode: this.emitForm.subjectCode,
                        landSubjectBase: this.form.subjectBase,
                        contractStartTime: formatDate(+new Date(this.formDate[0])),
                        lsSubjectUseArea: this.form.lsSubjectUseArea,
                        lsYearValue: this.form.lsYearValue,
                        contractEndTime: formatDate(+new Date(this.formDate[1])),
                        contractYear: this.form.contractYear,
                        planId: this.currentTask.data.id,
                        affiliatedUnit: this.form.affiliatedUnit,
                        contractId: this.form.contractId,
                        landSubjectBaseAdd: this.form.landSubjectBaseAdd,
                    },
                })
                .then((res) => {
                    if(res.errorCode === '00000'){

                        console.log(this.form,333333333333333333333333333);
            
                        this.$emit("on-success", this.form);
                        this.relevancySubjectShow = false;
                    }else{
                        this.$message({
                            message:res.errorMsg,
                            type:"warning"
                        })
                    }
     
                }).catch(()=>{})
        },
    },
};
</script>

<style lang="scss" scoped>
.result {
    margin: 10px 0;
    border: 1px solid #ddd;
    border-radius: 5px;
    height: 355px;

    h4 {
        padding-bottom: 15px;
        border-bottom: 1px solid #ddd;
        margin-bottom: 15px;
    }

    .title {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    p {
        line-height: 22px;
    }
}
</style>
